﻿function connectEvent(txt, button) {
    $(txt).attr('autocomplete', 'off');
    $(txt).keypress(function (event) {
        var keycode = (event.keyCode ? event.keyCode : event.which);
        if (keycode == '13') {
            $(button).click();
            return false;
        }
    });
}

// Vi du CallServerMethod("M.Common.Evaluate",{expression:"1+1"})
function CallServerMethod(methodPath, params, callback) {
    $.ajax({ url: "/Services/ExcuteServerMethod.ashx?methodPath=" + methodPath,
        type: "POST",
        data: (params),
        dataType: "html",
        success: callback
    });
}

function ToggleAttr(control, attrName, value1, value2) {
    if (control.attr(attrName) == value1) control.attr(attrName, value2);
    else control.attr(attrName, value1);
}

function ReplaceAll(Source, stringToFind, stringToReplace) {
    var temp = Source;
    var index = temp.indexOf(stringToFind);
    while (index != -1) {
        temp = temp.replace(stringToFind, stringToReplace);
        index = temp.indexOf(stringToFind);
    }
    return temp;
}
function GetPara(url, name) {
    var array = url.split(/\?|&/);
    var i = 0;
    for (i = 1; i < array.length; i++) {
        if (array[i].split('=')[0].toLowerCase() == name.toLowerCase()) return array[i].split('=')[1];
    }
    return '';
}
function SetPara(url, name, value) {
    var check = false;
    var isFirst = true;
    var ret = "";
    var array = url.split(/\?|&/);
    ret = ret + array[0];
    var i = 0;
    for (i = 1; i < array.length; i++) {
        var N = array[i].split('=')[0];
        var V = array[i].split('=')[1];
        if (N == name) {
            V = value;
            check = true;
        }
        ret = isFirst ? ret + "?" + N + "=" + V : ret + "&" + N + "=" + V;
        isFirst = false;
    }
    if (!check) ret = isFirst ? ret + "?" + name + "=" + value : ret + "&" + name + "=" + value;
    return ret;
}
function Contain(str, subStr) {
    index = str.indexOf(subStr);
    if (index == -1) return false;
    return true;
}
// abc => cba
function Revert(str) {
    var i = 0;
    var tem = "";
    for (i = str.length - 1; i >= 0; i--) {
        tem = tem + str[i];
    }
    return tem;
}
function SetHorrizalCenter(selector) {
    $(selector).get(0).style.left = (document.documentElement.clientWidth / 2 - $(selector).width() / 2) + 'px';
    //$(selector).get(0).style.top = (document.documentElement.clientHeight / 2 - $(selector).height() / 2) + 'px';
}
function SetVerticalCenter(selector) {
    //$(selector).get(0).style.left = (document.documentElement.clientWidth / 2 - $(selector).width() / 2) + 'px';
    $(selector).get(0).style.top = (document.documentElement.clientHeight / 2 - $(selector).height() / 2) + 'px';
}
function SetCenterScreen(selector) {
    $(selector).get(0).style.left = (document.documentElement.clientWidth / 2 - $(selector).width() / 2) + 'px';
    $(selector).get(0).style.top = (document.documentElement.clientHeight / 2 - $(selector).height() / 2) + 'px';
}
function showMask(targetSelector, zindex) {
    $(targetSelector).append("<div id='divMask' style='background-color: #AAAAAA;opacity: 0.5;position:absolute;z-index:" + zindex + "'></div>");
    $(targetSelector).find("div#divMask").get(0).style.left = $(targetSelector).offset().left + 'px';
    $(targetSelector).find("div#divMask").get(0).style.top = $(targetSelector).offset().top + 'px';
    $(targetSelector).find("div#divMask").width($(targetSelector).width());
    $(targetSelector).find("div#divMask").height($(targetSelector).height());
}
function hideMask(targetSelector) {
    $(targetSelector).find("div#divMask").remove();
}